package com.ynet.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import com.ynet.http.ForReq;

public class Register extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private JPanel jPanel;
	
	private JTextField userNameTextField;
	private JTextField passwordTextField;
	private JTextField passwordTextField2;
	private JTextField emailTextField;
	private JTextArea serverUrlTextField;
	
	private JButton btnNewButton;

//	public static String serverUrl;//服务器rul
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Register frame = new Register("http://127.0.0.1:18080/penetrate-server/");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Register(String serverUrl) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 282);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);

		ImageIcon ico = new ImageIcon("bananer.png");
		ico.setImage(ico.getImage().getScaledInstance(260, 100, Image.SCALE_DEFAULT));

		JLabel label = new JLabel(ico);
		panel.add(label);
		label.setFont(new Font("宋体", Font.PLAIN, 35));

		JPanel southPanel = new JPanel();
		contentPane.add(southPanel, BorderLayout.SOUTH);

		btnNewButton = new JButton("确定");
		btnNewButton.addActionListener(this);
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 12));
		southPanel.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("退出");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(-1);
			}
		});
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 12));
		southPanel.add(btnNewButton_1);

		JPanel centerPanel = new JPanel();
		contentPane.add(centerPanel, BorderLayout.CENTER);
		centerPanel.setLayout(new GridLayout(3, 0, 0, 0));

		JPanel userNamePanel = new JPanel();
		centerPanel.add(userNamePanel);

		JLabel userNmeLabel = new JLabel("用户名：");
		userNmeLabel.setFont(new Font("宋体", Font.PLAIN, 18));
		userNamePanel.add(userNmeLabel);

		userNameTextField = new JTextField();
		userNamePanel.add(userNameTextField);
		userNameTextField.setColumns(10);

		JPanel panel_6 = new JPanel();
		centerPanel.add(panel_6);

		JLabel passwordLabel = new JLabel("密  码：");
		passwordLabel.setFont(new Font("宋体", Font.PLAIN, 18));
		panel_6.add(passwordLabel);

		passwordTextField = new JTextField();
		panel_6.add(passwordTextField);
		passwordTextField.setColumns(10);

		JPanel panel_5 = new JPanel();
//		centerPanel.add(panel_5);

		JLabel lblNewLabel_2 = new JLabel("确认密码：");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 18));
		panel_5.add(lblNewLabel_2);

		passwordTextField2 = new JTextField();
		panel_5.add(passwordTextField2);
		passwordTextField2.setColumns(10);

		JPanel panel_4 = new JPanel();
//		centerPanel.add(panel_4);

		JLabel emailLabel = new JLabel("邮  箱：");
		emailLabel.setFont(new Font("宋体", Font.PLAIN, 18));
		panel_4.add(emailLabel);

		emailTextField = new JTextField();
		panel_4.add(emailTextField);
		emailTextField.setColumns(10);
		
		JPanel panel_1 = new JPanel();
		centerPanel.add(panel_1);
		panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JLabel label_1 = new JLabel("服务器URL：");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setFont(new Font("宋体", Font.PLAIN, 18));
		panel_1.add(label_1);
		
		serverUrlTextField = new JTextArea();
		serverUrlTextField.setText(serverUrl);
		serverUrlTextField.setRows(2);
		serverUrlTextField.setColumns(15);
		panel_1.add(serverUrlTextField);

		this.setTitle("绿灯-注册");
		this.setBounds(100, 100, 350, 250);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(true);
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == this.btnNewButton){
			Map<String, Object> dataMap = new ForReq().register(this.userNameTextField.getText(), this.passwordTextField.getText(), this.passwordTextField2.getText(),this.serverUrlTextField.getText());
			
			Object returnCode = dataMap.get("returnCode");
			Object returnMsg = dataMap.get("returnMsg");
			
			if(returnCode != null && !returnCode.equals("000000")){
				JOptionPane.showMessageDialog(jPanel, returnMsg, "错误提示",JOptionPane.WARNING_MESSAGE);  
			}else{
				JOptionPane.showMessageDialog(jPanel, "注册成！", "注册结果",JOptionPane.WARNING_MESSAGE);  
				
				Main.start(this.userNameTextField.getText(), this.passwordTextField.getText());
				this.setVisible(false);
				
			}
			
		}
		
	}
}
